package com.yj.springbootasync.rest;

import com.yj.springbootasync.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
@Slf4j
public class TestController {


    @Autowired
    TestService testService;

    @GetMapping("async")
    public String async() throws Exception {
        long start = System.currentTimeMillis();
        log.info("异步方法开始");

        Future future = testService.asyncMethod();
        String msg = (String) future.get();

        log.info("异步方法返回值：{}", msg);
        log.info("异步方法结束");

        long end = System.currentTimeMillis();

        log.info("总耗时：{}", end - start);

        return msg;
    }

    @GetMapping("sync")
    public void sync() {
        long start = System.currentTimeMillis();
        log.info("同步方法开始");
        testService.syncMethod();
        log.info("同步方法结束");
        long end = System.currentTimeMillis();
        log.info("总耗时：{}", end - start);
    }

}
